package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.Collection;

import javax.swing.AbstractAction;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

public class RosterViewer extends JPanel {
	private static final long serialVersionUID = 1L;
	
	private Roster _roster;
	
	private DefaultListModel _rosterModel;
	private JList _rosterList;
	private JScrollPane _rosterView;
	
	public RosterViewer(Roster roster){
		super(new BorderLayout());
		_roster = roster;
		
		initialize();
		connectRoster(roster);
	}
	
	public void connectRoster(Roster roster){
		roster.addRosterListener(new RosterListener(){
			@Override
			public void entriesAdded(Collection<String> addresses) {
				for (String entry : addresses) {
					_rosterModel.addElement(entry);
					_rosterList.revalidate();
				}
			}
			@Override
			public void entriesDeleted(Collection<String> addresses) {
				for (String entry : addresses) {
					_rosterModel.removeElement(entry);
					_rosterList.validate();
				}
			}
			@Override
			public void entriesUpdated(Collection<String> addresses) {
				_rosterList.validate();
			}
			@Override
			public void presenceChanged(Presence presence) {
//				String user = presence.getFrom();
//				Presence bestPresence = _roster.getPresence(user);
				
				_rosterView.repaint();
			}
		});
	}
	
	public void initialize(){
		_rosterModel = new DefaultListModel();
		
		for (RosterEntry entry : _roster.getEntries()) {
			_rosterModel.addElement(entry);
		}
		
		_rosterList = new JList(_rosterModel);
		_rosterList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		_rosterList.setCellRenderer(new DefaultListCellRenderer(){
		private static final long serialVersionUID = 1L;

		@Override
		public Component getListCellRendererComponent(
				JList list,Object value, int index, boolean isSelected, boolean cellHasFocus) {

			RosterEntry entry = (RosterEntry)value;
			Buddy2 buddy = new Buddy2(entry);
			buddy.setPresence(_roster.getPresence(entry.getUser()));

			if (isSelected) {
				buddy.setBackground(list.getSelectionBackground());
				buddy.setForeground(list.getSelectionForeground());
			} else {
				buddy.setBackground(list.getBackground());
				buddy.setForeground(list.getForeground());
			}
			return buddy;
		}});
		
		_rosterView = new JScrollPane(_rosterList);
		this.add(_rosterView, BorderLayout.CENTER);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
		buttonPanel.add(new JButton(new AbstractAction("Add"){
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg) {
//				try {
//					_roster.createEntry("", "", null);
//				} catch (XMPPException e) {
//					e.printStackTrace();
//				}
			}
		}));
		buttonPanel.add(new JButton(new AbstractAction("Remove"){
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent arg) {
				RosterEntry selection = RosterViewer.this.getSelectedRosterEntry();
				int result = JOptionPane.showConfirmDialog(RosterViewer.this, "Really want to remove "+selection.getName()+"?", "Confirmation...",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				if(result == JOptionPane.YES_OPTION){
					try {
						_roster.removeEntry(selection);
					} catch (XMPPException e) {
						e.printStackTrace();
					}
				}
				else{
					
				}
				
			}
		}));
		this.add(buttonPanel, BorderLayout.SOUTH);
		
		this.validate();
	}
	
	public void addSelectionListener(ListSelectionListener listener) {
		_rosterList.addListSelectionListener(listener);
	}

	public RosterEntry getSelectedRosterEntry() {
		return (RosterEntry)_rosterList.getSelectedValue();
	}
}
